package com.hxht.cmp.service.impl;

import com.hxht.cmp.entity.CourtRoom;
import com.hxht.cmp.mapper.CourtRoomMapper;
import com.hxht.cmp.service.CourtRoomService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourtRoomServiceImpl implements CourtRoomService {
    private final Logger logger = LoggerFactory.getLogger(CourtRoomServiceImpl.class);

    @Autowired
    private CourtRoomMapper courtRoomMapper;

    @Override
    public Map<String, String> getCfg(String courtRoomUid,String serverIp) {
        CourtRoom courtRoom = new CourtRoom();
        courtRoom.setUid(courtRoomUid);
        CourtRoom room = courtRoomMapper.selectOne(courtRoom);
        String cfg = room.getCfg();
        if (StringUtils.isEmpty(cfg)) {
            return null;
        }
        SAXReader saxReader = new SAXReader();
        Document document = null;
        Map<String, String> resultMap = new LinkedHashMap<String, String>();
        try {
            document = saxReader.read(new StringReader(cfg));
            Element root = document.getRootElement();
            logger.info("存储IP：" + root.attributeValue("localstoreip"));
            Element el = root.element("EncodeManage");
            if (el != null) {
                List<Element> list = el.elements("encode");
                for (Element e : list) {
                    String chnname = e.attributeValue("chnname");
                    String encode = e.attributeValue("encodeip") + "/" + e.attributeValue("encodeno");
                    resultMap.put(chnname, encode);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
